// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "PADInformer.h"

#include "SummaryView.h"
#include "DetailsView.h"
#include "MainFrm.h"
#include "Global.h"
#include "Init.h"
#include "Capture.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_COMMAND(ID_CAPTURE_STARTCAPTURE, OnCaptureStartcapture)
	ON_UPDATE_COMMAND_UI(ID_CAPTURE_STARTCAPTURE, OnUpdateCaptureStartcapture)
	ON_UPDATE_COMMAND_UI(ID_CAPTURE_STOPCAPTURE, OnUpdateCaptureStopcapture)
	ON_COMMAND(ID_CAPTURE_STOPCAPTURE, OnCaptureStopcapture)
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
	//}}AFX_MSG_MAP
	// Global help commands
	ON_COMMAND(ID_HELP_FINDER, CFrameWnd::OnHelpFinder)
	ON_COMMAND(ID_HELP, CFrameWnd::OnHelp)
	ON_COMMAND(ID_CONTEXT_HELP, CFrameWnd::OnContextHelp)
	ON_COMMAND(ID_DEFAULT_HELP, CFrameWnd::OnHelpFinder)
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
    m_pSummaryView = NULL;
    m_pDetailsView = NULL;
    m_isCapturing = false;
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);

  // Initialize Application
  Initialize();

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers
/*****************************************************************************/
/*                                                                           */
/* OnCreateClient                                                            */
/*                                                                           */
/*****************************************************************************/
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) 
{
  VERIFY(m_wndSplitter.CreateStatic(this, 1, 2));
  VERIFY(m_wndSplitter.CreateView(0,0, RUNTIME_CLASS(SummaryView),
                                  CSize(450,100), pContext));
  VERIFY(m_wndSplitter.CreateView(0,1, RUNTIME_CLASS(DetailsView),
                                  CSize(100,100), pContext));

  return TRUE;
}

/*****************************************************************************/
/*                                                                           */
/* OnFileOpen                                                                */
/*                                                                           */
/*****************************************************************************/
void CMainFrame::OnFileOpen() 
{
  int choice;
  char strFilename[200];
  CFileDialog fOpenDlg(true,                                 // true = open dialog
                       "cap",                                // default extension
                       NULL,                                 // default filename
                       OFN_FILEMUSTEXIST|OFN_HIDEREADONLY,   // creation flags
                       "PADI capture files (*.cap)|*.cap||", // file filter
                       this                                  // pointer to parent
                       );

  if (fOpenDlg.DoModal()==IDOK)
  {
    if (m_isCapturing)
    {
      choice = MessageBox("You currently have an open capture. Would you like to stop live capturing and open the file?",
                          "Are you sure?",
                          MB_YESNO|MB_ICONWARNING);
      if (choice == IDYES)
      {
        OnCaptureStopcapture();
      }
      else
        return;
    }
    ClearPacketList();
    m_pSummaryView->GetListCtrl().DeleteAllItems();
    strcpy(strFilename, fOpenDlg.GetPathName().GetBuffer(1));
    OpenCapture(strFilename);
  }
}

/*****************************************************************************/
/*                                                                           */
/* OnUpdateCaptureStartcapture                                               */
/*                                                                           */
/*****************************************************************************/
void CMainFrame::OnUpdateCaptureStartcapture(CCmdUI* pCmdUI) 
{
  pCmdUI->Enable(!m_isCapturing);
}

/*****************************************************************************/
/*                                                                           */
/* OnUpdateCaptureStopcapture                                                */
/*                                                                           */
/*****************************************************************************/
void CMainFrame::OnUpdateCaptureStopcapture(CCmdUI* pCmdUI) 
{
  pCmdUI->Enable(m_isCapturing);	
}

/*****************************************************************************/
/*                                                                           */
/* OnCaptureStartcapture                                                     */
/*                                                                           */
/*****************************************************************************/
void CMainFrame::OnCaptureStartcapture() 
{
  int choice;

  if (PacketList.GetCount() > 0)
  {
    choice = MessageBox("You currently have data being viewed. Would you like to delete this data and start a live capture?",
                        "Are you sure?",
                        MB_YESNO|MB_ICONWARNING);

    if (choice != IDYES)
      return;
  }

  m_isCapturing = true;
  hCaptureThread = CreateThread(NULL,            // Security Attributes
                                0,               // Stack Size
                                CaptureThread,   // Thread Function
                                0,               // Argument to the Thread Function
                                0,               // Creation Flags
                                &dwCaptureThread // Thread ID
                                );
}

/*****************************************************************************/
/*                                                                           */
/* OnCaptureStopcapture                                                      */
/*                                                                           */
/*****************************************************************************/
void CMainFrame::OnCaptureStopcapture() 
{
  m_isCapturing = false;
  pcap_close(fp);
}

